import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import BlogDriver
from tests.BlogList import BlogList
from tests.BlogLogin import BlogLogin


class BlogEdit:
    url= ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    # 登录状态下编辑博客功能测试
    def loginEditTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#submit")
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.editormd-preview")
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll")
        # 编辑一条博客并发布，时间较久，显示等待
        # 先测试空白标题是否能发布
        wait = WebDriverWait(self.driver,10)
        # 标题空白
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait.until(EC.alert_is_present())
        alter = self.driver.switch_to.alert
        alter.accept()

        # 标题有文字
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("测试用博客")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        BlogDriver.getScreenshot()

    # 未登录状态下点击发布以后应当直接跳转到登录页面
    def logoutEditTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")
        BlogDriver.getScreenshot()





